import type { Edge, Node } from "@vue-flow/core";
import { DataNodeType, FlowNodeType, type ConfigDataType } from "./enum"

export interface ConfigCol {
    id: number,
    name: string,
    type: number,
    value: any,
    key: string,
}
export const typeNames = ["整数", "浮点数", "文本", "开关", "模板实例"]
export interface ConfigTableHeader {
    id: number,
    label: string,
    type: ConfigDataType,
    key: string,
    quoteId: number,
    exportGroups: number[]
}

// 导出分组
export interface ExportGroup {
    id: number,
    name: string,
    desc: string
}

// 导出配表设置
export interface ExportTableSetting {
    tableId: number,
    exportColumnIds: number[],
    subTableSettings: ExportTableSetting[],
    dictKeyValues: number[]
}

// 导出设置
export interface ExportSetting {
    id: number,
    tableSettings: ExportTableSetting[],
    exportMethod: string,
    name: string,
    desc: string
}

export interface LanguageElement {
    id: number,
    name: string,
}

export interface TextElement {
    id: number;
    texts: Array<string>;
}

export interface OptionData {
    id: number,
    name: string,
    desc: string,
}

export interface OptionGroup {
    ids: number[],
    refCount: number,
    id: number,
}
export interface Choice {
    id: number,
    name: string,
    desc: string,
    options: Array<OptionData>,
    optionGroups: Map<number, number[]>,
}
export interface StoreChoice {
    id: number,
    name: string,
    desc: string,
    options: Array<OptionData>
    nextId: number,
    optionGroupNextId: number,
    optionGroups: Map<number, number[]>,
}

export interface StoreTable {
    nextHeaderId: number;
    exportGroups: number[],
    nextRowId: number,
    id: number,
    key: string,
    desc: string,
    tableHeader: Array<ConfigTableHeader>,
    rows: Array<TableRow>,
    name: string
}
export interface TableRow {
    id: number,
    values: Array<number>
}

export interface TableData {
    exportGroups: number[],
    id: number,
    key: string,
    desc: string,
    tableHeader: Array<ConfigTableHeader>,
    rows: Array<TableRow>,
    name: string
}

export interface TextAsyncData {
    languages: LanguageElement[],
    elements: TextElement[],
}

// 新增文本分组接口
export interface TextGroup {
    groupId: number;
    name: string;
    desc: string;
    elementIds: number[]; // 包含的文本元素ID数组
}

export interface StoreDataNode {
    id: number,
    type: DataNodeType,
    templateId: number,
    children: Array<StoreDataNode>,
    value: number,
    parentId: number,
}

export const dataNodeTypeNames = [
    { name: "整数", value: DataNodeType.Integer },
    { name: "浮点数", value: DataNodeType.Float },
    { name: "字符串", value: DataNodeType.String },
    { name: "布尔值", value: DataNodeType.Boolean },
    { name: "模板实例", value: DataNodeType.TemplateInst },
    { name: "对象", value: DataNodeType.Object },
    { name: "数组", value: DataNodeType.Array },
]
export const dataNodeTypeNamesMap = new Map<number, string>(dataNodeTypeNames.map(item => [item.value, item.name]))

export const BaseTypeMax = DataNodeType.Boolean

export interface FormulaArgument {
    id: number,
    key: string
}
export interface Formula {
    id: number,
    script: string,
    arguments: Array<FormulaArgument>,
    enabled: boolean
}
export const RootNodeId = -1
export const RootTemplateId = -1
export const AllTextGroupId = -1

export const BaseTypeNodes = [
    {id: DataNodeType.Integer, name: "整数"},
    {id: DataNodeType.Float, name: "浮点数"},
    {id: DataNodeType.Boolean, name: "布尔值"},
    {id: DataNodeType.String, name: "字符串"},
    {id: DataNodeType.Object, name: "对象"},
]

export interface FlowNodeData {
    name: string,
    flowId?: number
}

export interface FlowNode {
    id: string,
    position: {x: number, y: number},
    type: string,
    flowNodeType: FlowNodeType,
    data: FlowNodeData
}

export type FlowEdge = {
    id: string;
    source: string;
    target: string;
    animated?: boolean;
    type?: string;
    // 新增以下两个可选属性
    sourceHandle?: string; // 源节点连接的 handle 标识
    targetHandle?: string; // 目标节点连接的 handle 标识
};

export interface Flow {
    id: string,
    nextNodeId: number,
    nextEdgeId: number,
    name: string,
    desc: string,
    nodes: FlowNode[],
    edges: FlowEdge[],
    depth: number,
    referencedBy: string[]
}

export const FlowNodeTypeStrings = {
    [FlowNodeType.Start]: "start",
    [FlowNodeType.End]: "end",
    [FlowNodeType.Normal]: "normal",
    [FlowNodeType.Process]: "process",
    [FlowNodeType.Branch]: "branch",
    [FlowNodeType.Loop]: "loop",
    [FlowNodeType.Delay]: "delay",
    [FlowNodeType.Flow]: "flow"
}

export const FlowNodeTypeName = {
    [FlowNodeType.Start]: "开始节点",
    [FlowNodeType.End]: "结束节点",
    [FlowNodeType.Normal]: "普通节点",
    [FlowNodeType.Process]: "处理节点",
    [FlowNodeType.Branch]: "分支节点",
    [FlowNodeType.Loop]: "循环节点",
    [FlowNodeType.Delay]: "延时节点",
    [FlowNodeType.Flow]: "流程节点"
}

export const MainFlowId = 0